import React, { FC, useContext } from 'react';
import { View, Text, Button } from '@tarojs/components';
import { styled } from 'linaria/react';
import { useClassName } from '@/utils/index';
import { LoginContext } from '../../LoginContext';
import Taro from '@tarojs/taro';
import { apiGetUserLogin } from '@/pages/_server/_api/Login/apiGetUserLogin';
export interface IProps extends ICompProps {}
LoginBtn.defaultProps = {} as Partial<IProps>;
function LoginBtn(props: IProps) {
    const { className, style } = props;
    const localClassName = useClassName('LoginBtn', className);
    const { loginForm, isAgreeAgreement } = useContext(LoginContext);
    const login = async () => {
        if (!isAgreeAgreement)
            return Taro.showToast({
                title: '请先勾选同意协议',
                icon: 'none',
                duration: 2000,
            });
        let isValidatePass = await loginForm.validateFields(['phoneNumber', 'code']).then(
            (val) => {
                console.log('表单验证通过', val);
                return true;
            },
            (val) => {
                console.log('表单验证失败', val);
                return false;
            }
        );
        if (!isValidatePass) return;
        let res = await apiGetUserLogin(loginForm.getFieldsValue());
        if (!res) return;
        Taro.setStorageSync('token', res.data);
        Taro.navigateTo({
            url: 'pages/Home/Home',
        });
    };
    return (
        <RootView className={localClassName} style={style}>
            <View className="button">
                <Button onClick={login}>登录</Button>
            </View>
        </RootView>
    );
}
export default React.memo(LoginBtn as FC<IProps>);
const RootView = styled(View)`
    &.LoginBtn {
        height: 90px;
        .button {
            > .hydrated {
                color: #fff;
                font-size: 32px;
                letter-spacing: 4px;
                background: #ff8828 !important;
                border-radius: 50px;
            }
        }
    }
`;
